package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 虎哥
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryByParentId(Long pid) {
        // 创建Category对象，对象中的非空字段作为查询条件。
        Category record = new Category();
        record.setParentId(pid);
        // 发起查询请求
        List<Category> list = categoryMapper.select(record);
        if (CollectionUtils.isEmpty(list)) {
            // 资源不存在，返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        // 对象转换
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }

    public List<CategoryDTO> queryByIds(List<Long> ids){
        List<Category> list = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)) {
            // 资源不存在，返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        // 对象转换
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }
}
